<!DOCTYPE html>
<html>

<head>
    <title>Hello World</title>
    <script src="http://gosspublic.alicdn.com/aliyun-oss-sdk-6.0.2.min.js"></script>
    <script type="text/javascript" src="http://tst.dynamsoft.com/libs/dwt/14.3/dynamsoft.webtwain.min.js"> </script>
</head>

<body>
    <div id="dwtcontrolContainer"></div>
    <input type="button" value="Scan" onclick="scan()" />
    <input type="button" value="Upload to Aliyun OSS" onclick="upload()" />
    <script type="text/javascript">
        // Get the trial license from https://www.dynamsoft.com/CustomerPortal/Portal/Triallicense.aspx
        Dynamsoft.WebTwainEnv.ProductKey = '<Your DWT license>';
        function scan() {
            var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
            if (DWObject) {
                DWObject.SelectSource(function () {
                    var OnAcquireImageSuccess, OnAcquireImageFailure;
                    OnAcquireImageSuccess = OnAcquireImageFailure = function () {
                        DWObject.CloseSource();
                    };
                    DWObject.OpenSource();
                    DWObject.IfDisableSourceAfterAcquire = true;
                    DWObject.AcquireImage(OnAcquireImageSuccess, OnAcquireImageFailure);
                }, function () {
                    console.log('SelectSource failed!');
                });
            }
        }


        function asyncSuccessFunc(result) {
            console.log(result.size);

            let client = new OSS({
                accessKeyId: '<Your AccessKeyId>',
                accessKeySecret: '<Your AccessKeySecret>',
                bucket: 'Your bucket name',
                region: '<Your region>'
            });

            client.put('test.jpg', result).then(function (r1) {
                console.log('put success: %j', r1);
                return client.get('test.jpg');
            }).then(function (r2) {
                console.log('get success: %j', r2);
            }).catch(function (err) {
                console.error('error: %j', err);
            });
        }

        function asyncFailureFunc(errorCode, errorString) {
            alert("ErrorCode: " + errorCode + "\r" + "ErrorString:" + errorString);
        }

        function upload() {
            var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
            if (DWObject) {
                let currentIndex = DWObject.CapCurrentIndex;
                DWObject.ConvertToBlob([currentIndex], EnumDWT_ImageType.IT_JPG, asyncSuccessFunc,
                    asyncFailureFunc);

            }

        }
    </script>
</body>

</html>